package com.smartleanx.module.hrm.service.esign.temp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.framework.tenant.core.aop.TenantIgnore;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.esign.HrmETempDept;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmETempDeptMapper;
import com.smartleanx.module.hrm.enums.ErrorCodeConstants;
import com.smartleanx.module.hrm.eums.employee.BizFileTypeEnum;
import com.smartleanx.module.hrm.eums.employee.EmployeeStageFileEnum;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.service.esign.ESignUrlConstant;
import com.smartleanx.module.hrm.service.esign.eums.ContractCodeEnum;
import com.smartleanx.module.hrm.service.esign.service.*;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTempFileParam;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTempParam;
import com.smartleanx.module.hrm.service.esign.temp.po.*;
import com.smartleanx.module.hrm.service.esign.temp.result.*;
import com.smartleanx.module.hrm.service.esign.temp.vo.HrmEContractTempActorsVo;
import com.smartleanx.module.hrm.service.esign.temp.vo.HrmEContractTempVo;
import com.smartleanx.module.hrm.util.esign.ESignUtil;
import com.smartleanx.module.hrm.util.esign.EsignHttpResponse;
import com.smartleanx.module.hrm.util.esign.FileUtil;
import com.smartleanx.module.hrm.util.esign.enums.EsignRequestType;
import com.smartleanx.module.hrm.util.esign.enums.FileTypeEnums;
import com.smartleanx.module.hrm.util.esign.exception.EsignDemoException;
import com.smartleanx.module.system.api.customfield.CustomFieldApi;
import com.smartleanx.module.system.api.customfield.dto.CustomFieldArchiveDTO;
import com.smartleanx.module.system.api.customfield.dto.CustomFieldRespDTO;
import com.smartleanx.module.system.api.dept.DeptApi;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import org.redisson.misc.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;

@Service
public class ESignDocTempServiceImpl implements ESignDocTempService {

    @Value("${esign.url}")
    private String esignUrl;

    @Autowired
    private HrmEContractTempService hrmEContractTempService;
    @Autowired
    private HrmEContractTempActorsService hrmEContractTempActorsService;
    @Autowired
    private HrmEFolderService hrmEFolderService;
    @Autowired
    private HrmEFolderTempService hrmEFolderTempService;
    @Autowired
    private HrmETempFileService hrmETempFileService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private HrmECustomComponentsService hrmECustomComponentsService;
    @Autowired
    private HrmEFileService hrmEFileService;
    @Autowired
    private HrmEContractCodeService hrmEContractCodeService;
    @Autowired
    private DeptApi deptApi;
    @Autowired
    private HrmETempDeptService hrmETempDeptService;
    @Resource
    private CustomFieldApi customFieldApi;

    public void saveComponents(){
        CommonResult<List<CustomFieldRespDTO>> fieldList = customFieldApi.getFieldListByTenantId(1L);
        System.out.println("================固定字段==================");
        List<CustomFieldRespDTO> data = fieldList.getData();

        List<HrmECustomComponents> collect = data.stream().map(p -> {
            HrmECustomComponents hrmECustomComponents = new HrmECustomComponents();
            hrmECustomComponents.setDataStatus(1);
            hrmECustomComponents.setComponentKey(p.getId() + "");
            hrmECustomComponents.setTenantId(1L);
            hrmECustomComponents.setComponentName(p.getName());
            hrmECustomComponents.setComponentType(1);
            hrmECustomComponents.setType(p.getType());
            hrmECustomComponents.setOptions(p.getOptions());
            hrmECustomComponents.setSortNo(p.getId().intValue());
            return hrmECustomComponents;
        }).collect(Collectors.toList());
        for (HrmECustomComponents datum : collect) {
            System.out.println(datum);
        }

        hrmECustomComponentsService.saveBatch(collect);
    }

    @Override
    @TenantIgnore
    public void saveComponentToEsign() {

        saveComponents();

        String apiaddr=ESignUrlConstant.CUSTOM_COMPONENTS_CREATE;
        //请求方法
        EsignRequestType requestType= EsignRequestType.POST;

        List<HrmECustomComponents> componentsList = hrmECustomComponentsService.getComponentsList();
        JSONObject parram = new JSONObject();
        JSONArray jsonAr = new JSONArray();

        for (HrmECustomComponents components : componentsList) {
            JSONObject json = new JSONObject();
            json.set("componentName",components.getComponentName());
            json.set("customBizNum",components.getComponentKey());
            json.set("componentOrder",components.getSortNo());
            json.set("componentType",components.getComponentType());
            jsonAr.add(json);
        }
        parram.set("components",jsonAr);
        String jsonParm=parram.toString();

        EsignHttpResponse esignHttpResponse = ESignUtil.sendHttpResponse(jsonParm, requestType, apiaddr, esignUrl);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(esignHttpResponse.getBody(), JsonObject.class);
        if(jsonObject.get("code").getAsInt() != 0){
            throw exception(ErrorCodeConstants.E_SIGN_API_ERROR);
        }
        //2. 查询控件列表 保存数据
        JsonObject data = getEsignComponent();

        List<HrmECustomComponents> saveCustomComponents = new ArrayList<>();
        JsonArray components = data.getAsJsonArray("components");
        for (JsonElement component : components) {
            if(component.getAsJsonObject().get("customBizNum").isJsonNull()){
                continue;
            }
            String customBizNum = component.getAsJsonObject().get("customBizNum").getAsString();
            String componentId = component.getAsJsonObject().get("componentId").getAsString();
            HrmECustomComponents hrmECustomComponents = componentsList.parallelStream().filter(p -> p.getComponentKey().equals(customBizNum)).findAny().orElse(null);
            if(hrmECustomComponents == null){
                continue;
            }
            hrmECustomComponents.setComponentId(componentId);
            saveCustomComponents.add(hrmECustomComponents);
        }
        hrmECustomComponentsService.updateBatchById(saveCustomComponents);
    }

    @Override
    public JsonObject getEsignComponent() {
        String apiaddr=ESignUrlConstant.CUSTOM_COMPONENTS_LIST;
        //请求方法
        EsignRequestType requestType= EsignRequestType.POST;

        JSONObject parram = new JSONObject();
        parram.set("pageNum","1");
        parram.set("pageSize","100");
        String jsonParm=parram.toString();

        EsignHttpResponse esignHttpResponse = ESignUtil.sendHttpResponse(jsonParm, requestType, apiaddr, esignUrl);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(esignHttpResponse.getBody(), JsonObject.class);
        JsonObject data = jsonObject.getAsJsonObject("data");
        if(jsonObject.get("code").getAsInt() != 0){
            throw exception(ErrorCodeConstants.E_SIGN_API_ERROR);
        }

        return data;
    }

    @Override
    public void removeEsignComponent() {
        JsonObject data = getEsignComponent();
        JsonArray components = data.getAsJsonArray("components");

        String apiaddr=ESignUrlConstant.CUSTOM_COMPONENTS_REMOVE;
        //请求方法
        EsignRequestType requestType= EsignRequestType.POST;

        JSONObject parram = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (JsonElement component : components) {
            String componentId = component.getAsJsonObject().get("componentId").getAsString();
            jsonArray.add(componentId);
        }
        parram.set("componentId",jsonArray);
        String jsonParm=parram.toString();

        EsignHttpResponse esignHttpResponse = ESignUtil.sendHttpResponse(jsonParm, requestType, apiaddr, esignUrl);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(esignHttpResponse.getBody(), JsonObject.class);
        if(jsonObject.get("code").getAsInt() != 0){
            throw exception(ErrorCodeConstants.E_SIGN_API_ERROR);
        }
    }

    @Override
    public String uploadFile(HrmEContractTempFileParam fileParam) {
        Long fileId = null;
        String esignFileId = "";
        try {
            esignFileId = FileUtil.fileHTML(fileParam.getFileUrl());
        } catch (EsignDemoException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(StrUtil.isBlank(esignFileId)){
            throw exception(ErrorCodeConstants.UPLOAD_FILE_ERROR);
        }

//        HrmEFile hrmEFile = new HrmEFile();
//        hrmEFile.setDataStatus(1);
//        hrmEFile.setFileUrl(fileParam.getFilePath());
//        hrmEFile.setFileName(fileParam.getFileName());
//        hrmEFile.setType(fileParam.getFileType());
//        hrmEFile.setEFileId(esignFileId);
//        hrmEFileService.save(hrmEFile);
        return esignFileId;
    }

    @Override
    public List<HrmEFilesResult> getFiles() {
        // 返回前端的list
        List<HrmEFilesResult> result = new ArrayList<>();
        EmployeeStageFileEnum[] values = EmployeeStageFileEnum.values();
        for (EmployeeStageFileEnum e : values) {
            // 返回实体
            HrmEFilesResult respBo = new HrmEFilesResult();

            // 在职
            if (EmployeeStageFileEnum.EMPLOYEE_ON_THE_JOB.getCode().equals(e.getCode())) {
                respBo.setId(EmployeeStageFileEnum.EMPLOYEE_ON_THE_JOB.getCode().longValue());
                respBo.setFileTitle(EmployeeStageFileEnum.getName(EmployeeStageFileEnum.EMPLOYEE_ON_THE_JOB.getCode()));
                // 员工在职期间的所有文件类型
                List<Integer> onTheJobFileType = BizFileTypeEnum.getCodeByType(EmployeeStageFileEnum.EMPLOYEE_ON_THE_JOB.getCode());
                // 构建在职阶段返回前端的模板
                respBo.setChildren(curdEmployeeFileRef(onTheJobFileType));
                result.add(respBo);
            }
            if (EmployeeStageFileEnum.EMPLOYEE_LEAVE.getCode().equals(e.getCode())) {
                // 员工离职阶段的附件
                List<Integer> leave = BizFileTypeEnum.getCodeByType(EmployeeStageFileEnum.EMPLOYEE_LEAVE.getCode());

                // 离职其他
                respBo.setId(EmployeeStageFileEnum.EMPLOYEE_LEAVE.getCode().longValue());
                respBo.setFileTitle(EmployeeStageFileEnum.getName(EmployeeStageFileEnum.EMPLOYEE_LEAVE.getCode()));
                // 构建在职阶段返回前端的模板
                respBo.setChildren(curdEmployeeFileRef(leave));

                result.add(respBo);
            }

            if (EmployeeStageFileEnum.EMPLOYEE_ENTRY.getCode().equals(e.getCode())) {
                // 员工入职相关的附件
                List<Integer> entry = BizFileTypeEnum.getCodeByType(EmployeeStageFileEnum.EMPLOYEE_ENTRY.getCode());
                // 构建在职阶段返回前端的模板
                respBo.setId(EmployeeStageFileEnum.EMPLOYEE_ENTRY.getCode().longValue());
                respBo.setFileTitle(EmployeeStageFileEnum.getName(EmployeeStageFileEnum.EMPLOYEE_ENTRY.getCode()));
                respBo.setChildren(curdEmployeeFileRef(entry));
                result.add(respBo);
            }

        }
        return result;
    }

    /**
     * 构建返回前端的附件样式
     *
     * @param fileTypeList 附件类型 {@link  BizFileTypeEnum}
     * @return
     */
    public List<HrmEFilesResult> curdEmployeeFileRef(List<Integer> fileTypeList) {
        return CollectionUtils.convertList(fileTypeList, f -> {
            HrmEFilesResult refBo = new HrmEFilesResult();
            if (BizFileTypeEnum.isShow(f)) {
                refBo.setId(f.longValue());
                refBo.setFileTitle(BizFileTypeEnum.getName(f));
                return refBo;
            }
            return null;
        });
    }


    @Override
    public Long saveFile(HrmEContractTempFileParam fileParam) {
        Long fileId = null;
        //附件
        if(fileParam.getFileType() == 2){
            HrmEFile hrmEFile = new HrmEFile();
            hrmEFile.setDataStatus(1);
            hrmEFile.setFileUrl(fileParam.getFileUrl());
            hrmEFile.setFileName(fileParam.getFileName());
            hrmEFile.setType(fileParam.getFileType());
            hrmEFile.setEFileId(fileParam.getFileId());
            hrmEFileService.save(hrmEFile);
            fileId = hrmEFile.getId();
        }else if(fileParam.getFileType() == 1){
            String esignFileId = fileParam.getFileId();
            if(StrUtil.isBlank(esignFileId)){
                throw exception(ErrorCodeConstants.UPLOAD_FILE_ERROR);
            }
            HrmEFile hrmEFile = new HrmEFile();
            hrmEFile.setDataStatus(1);
            hrmEFile.setFileUrl(fileParam.getFileUrl());
            hrmEFile.setFileName(fileParam.getFileName());
            hrmEFile.setType(fileParam.getFileType());
            hrmEFile.setEFileId(esignFileId);
            hrmEFileService.save(hrmEFile);
            fileId = hrmEFile.getId();
        }

        return fileId;
    }

    @Override
    public List<HrmContractCodeResult> getContractCodeData(Long tempId) {
        List<HrmContractCodeResult> contractCodeTemp = getContractCodeTemp();
        if(tempId == null){
            return contractCodeTemp;
        }
        List<HrmEContractCode> contractCodeList = hrmEContractCodeService.getContractCodeBytempId(tempId);
        List<HrmContractCodeResult> result = contractCodeList.stream().map(p -> {
            HrmContractCodeResult hrmContractCodeResult = new HrmContractCodeResult();
            BeanUtil.copyProperties(p, hrmContractCodeResult);
            hrmContractCodeResult.setCodeName(p.getName());
            return hrmContractCodeResult;
        }).collect(Collectors.toList());
        return result;
    }

    public List<HrmContractCodeResult> getContractCodeTemp(){
        List<HrmContractCodeResult> hrmContractCodeResults = new ArrayList<>();
        for (ContractCodeEnum value : ContractCodeEnum.values()) {
            HrmContractCodeResult hrmContractCodeResult = new HrmContractCodeResult();
            hrmContractCodeResult.setCode(value.getCode());
            hrmContractCodeResult.setCodeName(value.getName());
            hrmContractCodeResult.setSortNo(value.getSortNo());
            hrmContractCodeResult.setSelected(value.getSelected());
            hrmContractCodeResults.add(hrmContractCodeResult);
        }

        return hrmContractCodeResults;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject saveDocTemp(HrmEContractTempVo hrmEContractTempVo) {
        JSONObject jsonObject = new JSONObject();
        //1. 获取模板编辑链接
        List<HrmEContractTempFileParam> signFileList = hrmEContractTempVo.getSignFileList();
        HrmEContractTempFileParam hrmEContractTempFileParam = signFileList.get(0);
        hrmEContractTempVo.setTempEsignFileId(hrmEContractTempFileParam.getFileId());

        JsonObject data = hrmEContractTempService.getDocTempLink(hrmEContractTempVo);
        if(null == data){
            throw exception(ErrorCodeConstants.E_TEMP_IS_NULL_ERROR);
        }
        String docTemplateCreateUrl = data.get("docTemplateCreateUrl").getAsString();
        String docTemplateId = data.get("docTemplateId").getAsString();
        //2. 添加模板信息
        HrmEContractTemp hrmEContractTemp = new HrmEContractTemp();
        BeanUtil.copyProperties(hrmEContractTempVo,hrmEContractTemp);
        hrmEContractTemp.setTempEsignId(docTemplateId);
        hrmEContractTemp.setTempStatus(0);
        hrmEContractTemp.setDataStatus(0);
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        EmployeeDO employee = employeeService.getEmployeeByUserId(loginUserId);
        hrmEContractTemp.setTempCreateName(employee.getName());
        String corpStr = hrmEContractTempVo.getCorpId().stream().map(String::valueOf).collect(Collectors.joining(","));
        hrmEContractTemp.setCorpId(corpStr);
        hrmEContractTemp.setCreateTime(LocalDateTime.now());
        hrmEContractTemp.setSaveUpdateStatus(0);

        CommonResult<List<DeptRespDTO>> deptList = deptApi.getDeptList(hrmEContractTempVo.getCorpId());
        String collect2 = deptList.getData().stream().map(DeptRespDTO::getName).collect(Collectors.joining(","));
        hrmEContractTemp.setCorpName(collect2);
        hrmEContractTempService.save(hrmEContractTemp);
        //添加权限表
        List<HrmETempDept> tempDeptSaveList = hrmEContractTempVo.getCorpId().stream().map(p -> {
            HrmETempDept hrmETempDept = new HrmETempDept();
            hrmETempDept.setTempId(hrmEContractTemp.getId());
            hrmETempDept.setDeptId(p.intValue());
            return hrmETempDept;
        }).collect(Collectors.toList());
        hrmETempDeptService.saveBatch(tempDeptSaveList);

        //3. 添加参与方信息
        List<HrmEContractTempActorsVo> actorsList = hrmEContractTempVo.getActorsList();
        hrmEContractTempActorsService.saveTempActor(actorsList,hrmEContractTemp.getId());
        //4. 添加文件夹关联信息
        List<Long> folderIdList = hrmEContractTempVo.getFolderIdList();
        if(hrmEContractTempVo.getFolderId() != null){
            if(CollUtil.isEmpty(folderIdList)){
                folderIdList = new ArrayList<>();
            }
            folderIdList.add(hrmEContractTempVo.getFolderId());
        }
        hrmEFolderTempService.saveList(folderIdList,hrmEContractTemp.getId());
        //5. 添加文件关联信息
        List<HrmEContractTempFileParam> attachmentFiles = hrmEContractTempVo.getAttachmentFileList();
        List<Long> attachmentFileList = new ArrayList<>();
        for (HrmEContractTempFileParam attachmentFile : attachmentFiles) {
            attachmentFile.setFileType(Integer.parseInt(FileTypeEnums.FILE_ATTACHMENT.getType()));
            Long aLong = saveFile(attachmentFile);
            attachmentFileList.add(aLong);
        }

        List<Long> signFileIdList = new ArrayList<>();
        for (HrmEContractTempFileParam attachmentFile : signFileList) {
            attachmentFile.setFileType(Integer.parseInt(FileTypeEnums.FILE_SIGN.getType()));
            Long aLong = saveFile(attachmentFile);
            signFileIdList.add(aLong);
        }


//        List<HrmEContractTempFileParam> fileList = hrmEContractTempVo.getFileList();
//
//        for (HrmEContractTempFileParam hrmEContractTempFileParam : fileList) {
//            hrmEContractTempFileParam.setFileId(hrmEContractTempVo.getTempEsignFileId());
//            Long aLong = saveFile(hrmEContractTempFileParam);
//            if(hrmEContractTempFileParam.getFileType() == Integer.parseInt(FileTypeEnums.FILE_SIGN.getType())){
//                fileIdList.add(aLong);
//            }else if(hrmEContractTempFileParam.getFileType() == Integer.parseInt(FileTypeEnums.FILE_ATTACHMENT.getType())){
//                attachmentFileList.add(aLong);
//            }
//        }
        hrmETempFileService.saveList(signFileIdList,hrmEContractTemp.getId(), FileTypeEnums.FILE_SIGN.getType());
        //6. 添加附件关联
        hrmETempFileService.saveList(attachmentFileList,hrmEContractTemp.getId(), FileTypeEnums.FILE_ATTACHMENT.getType());
//        7. 编号生成规则
        if(CollUtil.isNotEmpty(hrmEContractTempVo.getContractCodeList())){
            hrmEContractCodeService.saveBathCode(hrmEContractTempVo.getContractCodeList(),hrmEContractTemp.getId());
        }
        //8.插入轮询数据状态

        jsonObject.set("docTemplateCreateUrl",docTemplateCreateUrl);
        jsonObject.set("docTemplateId",docTemplateId);
        return jsonObject;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject updateDocTemp(HrmEContractTempVo hrmEContractTempVo) {
        JSONObject jsonObject = new JSONObject();
        Long tempId = hrmEContractTempVo.getId();
        //1. 获取模板编辑链接
        HrmEContractTemp contractTemp = hrmEContractTempService.getById(tempId);
        List<HrmEContractTempFileParam> signFileList = hrmEContractTempVo.getSignFileList();
        HrmEContractTempFileParam hrmEContractTempFileParam = signFileList.get(0);
        hrmEContractTempVo.setTempEsignFileId(hrmEContractTempFileParam.getFileId());
        hrmEContractTempVo.setTempEsignId(contractTemp.getTempEsignId());
        JsonObject data = hrmEContractTempService.getDocTempEditLink(hrmEContractTempVo);
        if(null == data){
            throw exception(ErrorCodeConstants.E_TEMP_IS_NULL_ERROR);
        }
        String docTemplateCreateUrl = data.get("docTemplateEditUrl").getAsString();
//        String docTemplateId = data.get("docTemplateId").getAsString();

        //1. 删除文件关联
        hrmETempFileService.removeTempFileByTempId(tempId);
        //2. 删除参与方
        hrmEContractTempActorsService.removeActorsByTempId(tempId);
        //2. 删除编码生成规则
        hrmEContractCodeService.removeContractCode(tempId);
        //3. 修改模板信息
        HrmEContractTemp hrmEContractTemp = new HrmEContractTemp();
        BeanUtil.copyProperties(hrmEContractTempVo,hrmEContractTemp);
//        hrmEContractTemp.setTempEsignId(contractTemp.getTempEsignId());
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        EmployeeDO employee = employeeService.getEmployeeByUserId(loginUserId);
        hrmEContractTemp.setTempUpdateName(employee.getName());
        String corpStr = hrmEContractTempVo.getCorpId().stream().map(String::valueOf).collect(Collectors.joining(","));
        hrmEContractTemp.setCorpId(corpStr);
        hrmEContractTemp.setSaveUpdateStatus(0);

        CommonResult<List<DeptRespDTO>> deptList = deptApi.getDeptList(hrmEContractTempVo.getCorpId());
        String collect2 = deptList.getData().stream().map(DeptRespDTO::getName).collect(Collectors.joining(","));
        hrmEContractTemp.setCorpName(collect2);
        hrmEContractTempService.updateById(hrmEContractTemp);
        //4. 添加参与方信息
        hrmEContractTempActorsService.saveTempActor(hrmEContractTempVo.getActorsList(),tempId);
        //5. 添加文件夹关联信息
        List<Long> folderIdList = hrmEContractTempVo.getFolderIdList();
        if(hrmEContractTempVo.getFolderId() != null){
            if(CollUtil.isEmpty(folderIdList)){
                folderIdList = new ArrayList<>();
            }
            folderIdList.add(hrmEContractTempVo.getFolderId());
        }
        hrmEFolderTempService.saveList(folderIdList,hrmEContractTemp.getId());
        //6. 添加文件关联信息
        List<HrmEContractTempFileParam> attachmentFiles = hrmEContractTempVo.getAttachmentFileList();
        List<Long> attachmentFileList = new ArrayList<>();
        for (HrmEContractTempFileParam attachmentFile : attachmentFiles) {
            attachmentFile.setFileType(Integer.parseInt(FileTypeEnums.FILE_ATTACHMENT.getType()));
            Long aLong = saveFile(attachmentFile);
            attachmentFileList.add(aLong);
        }

        List<Long> signFileIdList = new ArrayList<>();
        for (HrmEContractTempFileParam attachmentFile : signFileList) {
            attachmentFile.setFileType(Integer.parseInt(FileTypeEnums.FILE_SIGN.getType()));
            Long aLong = saveFile(attachmentFile);
            signFileIdList.add(aLong);
        }
        hrmETempFileService.saveList(signFileIdList,hrmEContractTemp.getId(), FileTypeEnums.FILE_SIGN.getType());
        //7. 添加附件关联
        hrmETempFileService.saveList(attachmentFileList,hrmEContractTemp.getId(), FileTypeEnums.FILE_ATTACHMENT.getType());
        //8. 编号生成规则
        if(CollUtil.isNotEmpty(hrmEContractTempVo.getContractCodeList())){
            hrmEContractCodeService.saveBathCode(hrmEContractTempVo.getContractCodeList(),hrmEContractTemp.getId());
        }



        jsonObject.set("docTemplateCreateUrl",docTemplateCreateUrl);
        jsonObject.set("docTemplateId",contractTemp.getTempEsignId());
        return jsonObject;
    }

    @Override
    public PageResult<HrmEContractTempResult> getDocTempPage(HrmEContractTempParam param) {
        return hrmEContractTempService.getDocTempPage(param);
    }

    @Override
    public HrmEContractTempDetailResult getDocTempDetail(Long id) {
        return hrmEContractTempService.getDocTempDetail(id);
    }

    @Override
    public boolean updateDocTempStatus(HrmEContractTempParam param) {
        return hrmEContractTempService.updateDocTempStatus(param);
    }

    @Override
    public boolean removeDocTemp(HrmEContractTempParam param) {
        return hrmEContractTempService.removeDocTemp(param);
    }

    @Override
    public boolean saveFolder(HrmEFolder hrmEFolder) {
        if(hrmEFolder.getParentId() == null){
            hrmEFolder.setParentId(0L);
        }
        return hrmEFolderService.save(hrmEFolder);
    }

    @Override
    public boolean updateFolder(HrmEFolder hrmEFolder) {
        return hrmEFolderService.updateById(hrmEFolder);
    }

    @Override
    public boolean removeFolder(HrmEFolder hrmEFolder) {
        return hrmEFolderService.removeFolder(hrmEFolder);
    }

    @Override
    public HrmEFolder getFolderDetail(Long id) {
        return hrmEFolderService.getFolderDetail(id);
    }


    @Override
    public List<HrmEFolderResult> getFolderResult(String type) {
        return hrmEFolderService.getFolderResult(type);
    }

    @Override
    public List<HrmEFolderResult> getParentFolderResult(String type) {
        return hrmEFolderService.getParentFolderResult(type);
    }

    @Override
    public Integer getSaveUpdateStatus(String tempSignId) {
        HrmEContractTemp hrmEContractTemp = hrmEContractTempService.getDocTempBySignId(tempSignId);
        return hrmEContractTemp.getSaveUpdateStatus();
    }

}
